/**
 * GULP Required components
 */
const gulp = require('gulp');
const babel = require('gulp-babel');
const bowerfiles = require('main-bower-files');
const es = require('event-stream');
const del = require('del');
const header = require('gulp-header');
const footer = require('gulp-footer');
const concat = require('gulp-concat');
const sass = require('gulp-sass');
const minifyCss = require('gulp-minify-css');
const autoprefixer = require('gulp-autoprefixer');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
const inject = require('gulp-inject');
const glob = require('glob');

const bower_src = './bower_components/**';
const src_path = './www/src/';
const build_path = './www/dist/';
const src_js_path = src_path + 'zhx.homepage.js';
const src_sass_path = src_path + 'zhx.homepage.scss';

const index_path = './www/index.html';
const lib_js_path = [
    './www/lib/jquery/dist/jquery.js',
    './www/lib/angular/angular.js',
     './www/lib/oclazyload/dist/ocLazyLoad.js',
     './www/lib/jquery-ui/jquery-ui.js',
     './www/lib/lodash/dist/lodash.min.js',
     './www/lib/gridstack/dist/gridstack.css',
     './www/lib/gridstack/dist/gridstack.min.js',
     './www/lib/gridstack-angular/dist/gridstack-angular.js',
     './www/lib/echarts/echarts.min.js',
     './www/lib/angular-bootstrap-contextmenu/contextMenu.js',
     './www/lib/velocity/velocity.min.js',
     './www/lib/zhx-loading/zhx-loading.min.css',
     './www/lib/zhx-loading/zhx-loading.js',
     './www/lib/paginations/tm.pagination.css',
     './www/lib/paginations/tm.pagination.js',
     './www/lib/zhx-data-grid/dist/zhx-data-grid.min.css',
     './www/lib/zhx-data-grid/dist/zhx-data-grid.js',
     build_path + '/*',
    './www/app.js'
];
const gridJs = [
    './www/lib/zhx-data-grid/src/base.js',
    './www/lib/zhx-data-grid/src/grid-utils.js',
    './www/lib/zhx-data-grid/src/grid-service.js',
    './www/lib/zhx-data-grid/src/grid.js',
    './www/lib/zhx-data-grid/src/grid-render-row.js',
    './www/lib/zhx-data-grid/src/grid-render-cell.js',
    './www/lib/zhx-data-grid/src/grid-check.js',
    './www/lib/zhx-data-grid/src/grid-order.js',
    './www/lib/zhx-data-grid/src/grid-column-drag.js',
    './www/lib/zhx-data-grid/src/grid-column-hide.js',
    './www/lib/zhx-data-grid/src/base-extend.js'
];

gulp.task('makeIndex',function(){
    gulp.src(bower_src).pipe(gulp.dest('./www/lib'));

    gulp.src(index_path)
        .pipe(inject(gulp.src( lib_js_path, {read:false} ), {name:'zhx',relative:true}))
        .pipe(gulp.dest('./www'))
});


gulp.task('build', function(){
    gulp.src(src_js_path)
    // .pipe(babel({
    //     presets: [
    //         ['es2015',{modules: false}]
    //     ]
    // }))
    // .pipe(concat('zhx.homepage.js'))
        .pipe( header('(function(window){ \n') )
        .pipe( footer('\n })(window); ') )
        .pipe(gulp.dest('./www/dist'));

    gulp.src(src_sass_path)
        .pipe(sass())
        .pipe(concat('zhx.homepage.css'))
        .pipe(autoprefixer())
        .pipe(gulp.dest( build_path ));

    gulp.src(src_path + 'font/*')
        .pipe( gulp.dest(build_path + "font") );
});

gulp.task('gridJs',function(){
    gulp.src(gridJs)
        .pipe( concat('zhx-data-grid.js') )
        .pipe( header('(function(){ \n') )
        .pipe( footer('\n })(); ') )
        .pipe( gulp.dest('./www/lib/zhx-data-grid/dist') )
        .pipe( uglify() )
        .pipe( rename({ extname: '.min.js' }) )
        .pipe( gulp.dest('./www/lib/zhx-data-grid/dist') )
});

gulp.task('gridSass',function(){
    gulp.src('./www/lib/zhx-data-grid/src/zhx-data-grid.scss')
        .pipe( sass() )
        .pipe( gulp.dest('./www/lib/zhx-data-grid/dist/') )
        .pipe( minifyCss() )
        .pipe( rename({ extname : '.min.css'} ) )
        .pipe( gulp.dest('./www/lib/zhx-data-grid/dist/') );

    gulp.src('./www/lib/zhx-data-grid/src/zhx-data-grid-font/*')
        .pipe(gulp.dest('./www/lib/zhx-data-grid/dist/zhx-data-grid-font/'));

});

gulp.task('watch', function(){
    gulp.watch('./www/src/*.*', ['build']);
    // gulp.watch(gridJs, ['gridJs']);
});